package com.example.origin.spider.service;

import com.example.origin.spider.webmagic.ChromeProcessor;
import com.example.origin.spider.webmagic.SimpleChromeDownloader;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

import java.util.List;
import java.util.Map;

@Service("trading-economics")
public final class TradingeConomicsProcessor extends ChromeProcessor {

    @Override
    public void process(ResultItems resultItems, Task task) {
        System.out.println("get page: " + resultItems.getRequest().getUrl());
        for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
            System.out.println(entry.getKey() + ":\t" + entry.getValue());
        }
    }

    @Override
    public void process(Page page) {
//        if (page.getUrl().get().contains("ip.chaxunjiao.com")){
//            page.putField("ip", page.getHtml().css("h3").get());
//            return;
//        }else {
//            //解折返回的数据page,并目把解折的结果放到ResultItems中
//            page.putField("div", page.getHtml().css("div.dt a").all());
//        }
//
//        page.addTargetRequest("https://ip.chaxunjiao.com/");

//        Selectable links = page.getHtml().links();
//        for (String s : links.all()) {
//            System.out.println(s);
//        }
        SimpleChromeDownloader downloader = (SimpleChromeDownloader)page.getRequest().getDownloader();
        WebDriver driver = downloader.getWebDriver();


//        List<Selectable> nodes = page.getHtml().css(".highcharts-point").nodes();
//        for (Selectable node : nodes) {
//            String s = node.get();
//            System.out.println(s);
//        }

        WebElement titleEle = driver.findElement(By.cssSelector(".title-indicator"));

        List<WebElement> elements = driver.findElements(By.cssSelector(".highcharts-point"));
        for (WebElement element : elements) {

            // 创建Actions对象
            Actions builder = new Actions(driver);

            // 使用moveToElement方法模拟鼠标悬停
            builder.moveToElement(element).perform();


            WebElement el = driver.findElement(By.cssSelector(".tooltip-value"));
            String text = el.getText();
            el = driver.findElement(By.cssSelector(".tooltip-date"));
            String text2 = el.getText();
            System.out.println(text2+"  "+text);

            builder.moveToElement(titleEle).perform();

        }

    }

}
